GOHOSTOS:=$(shell go env GOHOSTOS)
GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)
APP_SERVICES=(transfer-service bird-service transfer-service media-service ai-service gateway-service)

ifeq ($(GOHOSTOS), windows)
	#the `find.exe` is different from `find` in bash/shell.
	#to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
	#changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
	#Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
	#Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
	Git_Bash=$(subst \,/,$(subst cmd\git.exe,bin\bash.exe,"$(shell where git)"))
	INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find app/transfer-service/internal -name *.proto")
	API_PROTO_FILES=$(shell $(Git_Bash) -c "find proto_repo -name *.proto")
else
	INTERNAL_PROTO_FILES=$(shell find app/*/internal -name *.proto)
	API_PROTO_FILES=$(shell find proto_repo -name *.proto)
endif

# make wire
wire:
	cd app/transfer-service/cmd/server && wire

.PHONY: init
# init env
init:
	go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
	go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
	go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
	go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
	go install github.com/google/wire/cmd/wire@latest

.PHONY: config
# generate internal proto
config:
	protoc --proto_path=./app/transfer-service/internal \
	       --proto_path=./proto_repo/third_party \
 	       --go_out=paths=source_relative:./app/transfer-service/internal \
	       app/transfer-service/internal/conf/transfer_srv_conf.proto

.PHONY: tidy
# generate api proto
tidy:
	cd ./app/ai-service/cmd/server && go mod tidy
	cd ./app/transfer-service/cmd/server && go mod tidy
	cd ./app/bird-service/cmd/server && go mod tidy
	cd ./app/transfer-service/cmd/server && go mod tidy
	cd ./app/media-service/cmd/server && go mod tidy
	cd ./app/gateway-service/cmd/server && go mod tidy


.PHONY: api
# generate api proto
api:
	protoc --proto_path=./proto_repo/biz \
	       --proto_path=./proto_repo/third_party \
	       --go_out=paths=source_relative:./api \
	       --go-http_out=paths=source_relative:./api  \
	       --go-grpc_out=paths=source_relative:./api  \
	       --openapi_out=fq_schema_naming=true,default_response=false:. \
	       $(filter-out proto_repo/third_party/% || proto_repo/biz/common/%, $(API_PROTO_FILES))


.PHONY: build
# build
build:
	mkdir -p bin/ && GOOS=$(GOHOSTOS) GOARCH=amd64
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/transfer-service.exe ./app/transfer-service/cmd/server
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/bird-service.exe ./app/bird-service/cmd/server
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/transfer-service.exe ./app/transfer-service/cmd/server
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/media-service.exe ./app/media-service/cmd/server
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ai-service.exe ./app/ai-service/cmd/server
	go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/gateway-service.exe ./app/gateway-service/cmd/server

SERVICES := base bird device media ai gateway
linux:
	@# 输出到 bin
	@rm -rf bin
	@mkdir -p bin/
	# 编译和配置
	@for service in $(SERVICES); do \
		GOOS=linux GOARCH=amd64 go build -o ./bin/linux/$$service-service/$$service-service ./app/$$service-service/cmd/server; \
		cp -rf ./app/$$service-service/configs ./bin/linux/$$service-service; \
		cd ./bin/linux && tar -zcf $$service-service.tar.gz $$service-service && cd ../../; \
		rm -rf bin/linux/$$service-service; \
	done
	# 复制pkg
	@mkdir -p bin/linux/pkg/
	@cp -rf ./pkg/script/ ./bin/linux/pkg/
	@cp -rf ./pkg/model/ ./bin/linux/pkg/
	@mkdir -p bin/linux/pkg/ai_live
	@cp -f ./pkg/bird_model/live_flv.py ./bin/linux/pkg/ai_live
	# 复制supervisord
	@cp -rf ./deploy/supervisord.d	./bin/linux
	# 打包所有
	@mv ./bin/linux ./bin/bird-airport
	@cd ./bin/ && tar -zcf bird-airport.tar.gz bird-airport
	@rm -rf bin/bird-airport

.PHONY: generate
# generate
generate:
	go mod tidy
	go get github.com/google/wire/cmd/wire@latest
	go generate ./...

docker:
	docker build -f app/transfer-service/Dockerfile -t transfer-service .


.PHONY: all
# generate all
all:
	make api;
	make config;
	make generate;

.PHONY: run
# generate all
run:
	cd app/transfer-service/cmd/server && kratos run
	cd app/bird-service/cmd/server && kratos run
	cd app/transfer-service/cmd/server && kratos run

# show help
help:
	@echo ''
	@echo 'Usage:'
	@echo ' make [target]'
	@echo ''
	@echo 'Targets:'
	@awk '/^[a-zA-Z\-\_0-9]+:/ { \
	helpMessage = match(lastLine, /^# (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")); \
			helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
			printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help

compress:
	@echo "Saving Docker image..."
	cd ./app/transfer-service && docker save -o transfer-service.tar transfer-service:latest
	@echo "Creating compressed package..."
	cd ./app/transfer-service && tar -czvf transfer-service-package.tar.gz configs docker-compose.yaml transfer-service.tar && del /f /q transfer-service.tar

